import Vue from "vue"
import Vuex from 'vuex'
import userModules from './modules/user.js';
import systemModules from './modules/system.js';
import formViewModules from './modules/formView.js';

Vue.use(Vuex)

const store = new Vuex.Store({
	modules: {
		formView: formViewModules,
		system: systemModules,
		user: userModules
	},
	mutations: {
		resetState(state) {
			// 页面刷新store会被重置,需要更新回数据
			let token = uni.getStorageSync('token');
			let user = uni.getStorageSync('userInfo');
			let areaId = uni.getStorageSync('areaId');
			let areaName = uni.getStorageSync('areaName');
			let latitude = uni.getStorageSync('latitude');
			let longitude = uni.getStorageSync('longitude');
			let indexActive = uni.getStorageSync('indexActive')
			let applyFormList = uni.getStorageSync('applyFormList')
			let historyActive = uni.getStorageSync('historyActive')
			if (token) state.user.token = token;
			if (user) {
				state.user.userId = user.id;
				state.user.phone = user.phone;
				state.user.userName = user.nickName;
				state.user.avatarUrl = user.avatarUrl;
			}
			if (areaId) {
				state.system.areaId = areaId
				state.system.areaName = areaName
			}
			if (latitude && longitude) {
				state.user.lat = latitude
				state.user.lng = longitude
			}
			if (indexActive) {
				state.formView.indexActive = indexActive
			}
			if (historyActive) {
				state.formView.historyActive = historyActive
			}
			if (applyFormList) {
				state.formView.applyFormList = applyFormList
			}
			console.log("store内容被回填")
		}
	}
})

export default store